package dbhandler;

import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class Country {

    private String countryCode;
    private String countryName;

    /**
     *
     */
    public Country() {
    }

    /**
     *
     * @param countryCode
     */
    public Country(String countryCode) {
        this.countryCode = countryCode;
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT countryName FROM country WHERE countryCode='" + countryCode + "'");
            while (rs.next()) {
                countryName = rs.getString("countryName");
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Country", "There was an error performing the sql inquery\n").showException(ex);
        }
    }

    /**
     *
     * @return
     */
    public ArrayList<Country> getCountries() {
        ArrayList<Country> countries = new ArrayList<Country>();
        ArrayList<String> temp = new ArrayList<String>();
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT countryCode FROM country ORDER BY countryName");
            while (rs.next()) {
                temp.add(rs.getString("countryCode"));
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Country.getCountries()", "There was an error performing the sql inquery\n").showException(ex);
        }
        for (int i = 0; i < temp.size(); i++) {
            countries.add(new Country(temp.get(i)));
        }
        return countries;
    }

    /**
     *
     * @return
     */
    public String getCountryCode() {
        return countryCode;
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return countryName;
    }
}
